﻿using UnityEngine;
using System.Collections.Generic;
using Dict.DataModel;

namespace UI
{
    public class UILuckHeroTemplate : UILuckTemplate
    {
        #region 框图标
        public const string blue  = "UI_kuang_big_yuandi_blue";
        public const string green = "UI_kuang_big_yuandi_green";
        public const string red   = "UI_kuang_big_yuandi_red";
        public const string white = "UI_kuang_big_yuandi_white";
        public const string yellow ="UI_kuang_big_yuandi_yellow";
        public const string zi    = "UI_kuang_big_yuandi_zi";
        #endregion

        public UITexture heroIcon;
        public UISprite heroFrame;
        public GameObject mask;
        public GameObject actived;

        #region 数据
        protected List<UIHero> heros;
        protected List<string> heroIds;
        protected List<bool> masked;
        #endregion


        public override void DictData()
        {
            heros = new List<UIHero>();
            heroIds = new List<string>();
            masked = new List<bool>();

            if (!string.IsNullOrEmpty(luck.luckData.hero0)) heroIds.Add(luck.luckData.hero0);
            if (!string.IsNullOrEmpty(luck.luckData.hero1)) heroIds.Add(luck.luckData.hero1);
            if (!string.IsNullOrEmpty(luck.luckData.hero2)) heroIds.Add(luck.luckData.hero2);
            if (!string.IsNullOrEmpty(luck.luckData.hero3)) heroIds.Add(luck.luckData.hero3);

            //有英雄已经上阵或者助阵
            foreach (var heroId in heroIds)
            {
                foreach(var hero in StaticData.playerData.InstPlayerData.BattleCamps)
                {
                    if(hero.Value!=null && hero.Value.hero!=null && hero.Value.hero.heroId == heroId)
                    {
                        heros.Add(hero.Value.hero);
                        masked.Add(false);
                        break;
                    }
                }

                foreach(var help in StaticData.playerData.InstPlayerData.GetHelpCamps())
                {
                    UIHero hero = StaticData.playerData.GetUIHero(help.Value.guid);
                    if(hero!=null && hero.heroId == heroId)
                    {
                        heros.Add(hero);
                        masked.Add(false);
                        break;
                    }
                }
            }

            
            foreach(var heroId in heroIds)
            {
                bool hasUsed = false;
                foreach(var hero in heros)
                {
                    if(hero.heroId == heroId)
                    {
                        hasUsed = true;
                        break;
                    }
                }

                if(!hasUsed)
                {
                    bool has = false;
                    foreach(var hero in StaticData.playerData.InstPlayerData.InstHeros)
                    {
                        if(hero.Value.heroId == heroId)
                        {
                            heros.Add(hero.Value);
                            has = true;
                            break;
                        }
                    }

                    if(!has)
                    {
                        UIHero newHero = new UIHero(heroId);
                        heros.Add(newHero);  
                    }
                    masked.Add(true);
                }
            }
        }

        public override void SetIcon()
        {
            var heroIconRes = heros[0].heroData.iconName;
            MainTexture(heroIcon,heroIconRes);
            heroFrame.spriteName = GetFrameRes(heros[0].heroData.quality);
        }

        protected override void Actived(bool yes0, bool yes1 = false, bool yes2 = false)
        {
            actived.SetActive(yes0);
        }

        protected override void Masked(bool yes0, bool yes1 = false, bool yes2 = false)
        {
            mask.SetActive(yes0);
        }
        

        public string GetFrameRes(int quality)
        {
            var qual = (QualityValue)quality;
            switch (qual)
            {
                case QualityValue.BLUE:
                    return blue;
                case QualityValue.GREEN:
                    return green;
                case QualityValue.PURPLE:
                    return zi;
                case QualityValue.RED:
                    return red;
                case QualityValue.WHITE:
                    return white;
                case QualityValue.YELLOW:
                    return yellow;
            }

            return white;
        }

        protected override void MainTexture(UITexture tex, string iconName)
        {
            tex.GetComponent<TextureKit>().iconType = TextureIcon.HERO;
            tex.GetComponent<TextureKit>().textureName = iconName;
        }
        
        protected bool GetGuid(string id,out string guid)
        {
            var heros = StaticData.playerData.InstPlayerData.InstHeros;
            foreach(var hero in heros)
            {
                if(hero.Value.heroId == id)
                {
                    guid = hero.Value.heroId;
                    return true;
                }
            }

            guid = null;
            return false;
        }

        public override void InitMasked()
        {
            if (!center)
            {
                SetMasked(masked);
            }
        }

        public override void OnClick()
        {
            CommonMethod.ShowDescription(UIItemType.Hero, heros[0].heroId);
        }
    }
}
